/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.matrimonybeans;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;

/**
 *
 * @author hb
 */
@Stateless
public class Update_Charges implements Update_ChargesLocal {

    Connection conn;
    boolean flag = false;
    ResultSet rs = null;

    @Override
    public Boolean Update_Charges_data(String duration, BigDecimal price, int id_charges) {
        try {
            ConnectDB conDB = new ConnectDB();
            conn = conDB.getConnection();
            String sql_update = "Update Charges set duration=?, price= ? where id_charges=?";
            PreparedStatement pmst = conn.prepareStatement(sql_update);

            pmst.setString(1, duration);
            pmst.setBigDecimal(2, price);
            pmst.setInt(3, id_charges);

            int count = 0;
            count = pmst.executeUpdate();
            if (count > 0) {
                flag = true;
            } else {
                flag = false;
            }
        } catch (SQLException ex) {
            Logger.getLogger(Update_Charges.class.getName()).log(Level.SEVERE, null, ex);
        }
        return flag;
    }

    
    @Override
    public HashMap SelectCharges() {
        HashMap selectCharge = new HashMap();
        try {
            ConnectDB conDB = new ConnectDB();
            conn = conDB.getConnection();
            String select_Charges = "Select * from Charges";
            PreparedStatement pmst = conn.prepareStatement(select_Charges);
            rs = pmst.executeQuery();
            while(rs.next())
            {
                HashMap hm = new HashMap();
                hm.put("id_charges", rs.getInt("id_charges"));
                hm.put("duration", rs.getString("duration").trim());
                hm.put("price", rs.getInt("price"));
                selectCharge.put(rs.getInt("id_charges"),hm);
                
            }
        } catch (SQLException ex) {
            Logger.getLogger(Update_Charges.class.getName()).log(Level.SEVERE, null, ex);
        }
        return selectCharge;
    }

    @Override
    public ResultSet businessMethodSelectCharges(int id_charges) {
        try {
            ConnectDB conDB = new ConnectDB();
             conn = conDB.getConnection();
             String select_Charge = "Select * from Charges where id_charges= ?";
             PreparedStatement pmst = conn.prepareStatement(select_Charge);
             pmst.setInt(1, id_charges);
             rs = pmst.executeQuery();
        } catch (SQLException ex) {
            Logger.getLogger(Update_Charges.class.getName()).log(Level.SEVERE, null, ex);
        }
        return rs;
    }

    @Override
    public Boolean businessMethodDeleteCharges(int id_charges) {
        try {
            ConnectDB conDB = new ConnectDB();
               conn = conDB.getConnection();
               String delte_Charge = "Delete Charges where id_charges= ?";
               PreparedStatement pmst = conn.prepareStatement(delte_Charge);
               pmst.setInt(1, id_charges);
               
               int count =0;
               count = pmst.executeUpdate();
               if(count > 0)
               {
                   flag = true;
               }
               else{
                   flag = false;
               }
        } catch (SQLException ex) {
            Logger.getLogger(Update_Charges.class.getName()).log(Level.SEVERE, null, ex);
        }
        return flag;
    }
    
    
}
